﻿namespace Web.Control.WWW.Resources
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.ControlsUI;    
    using System.Collections.Generic;
    using BLF.WWW_WWW.Resource;
    using System.Collections;
    using Model.WWW_WWW.Resource;

    /// <summary>
    /// ResList 的摘要说明
    /// </summary>
    public class ResList : BaseControl
    {
        /// <summary>
        /// 学习语言
        /// </summary>
        protected string LearnL = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string SpeakL = "";
        /// <summary>
        /// 当前会员是否为管理员(1是，0否)
        /// </summary>
        protected int IsAdmin = 0;
        /// <summary>
        /// 资源类型绑定
        /// </summary>
        protected Repeater rptCat;
        protected Repeater rptCatForRes;
        protected Repeater rptAdmin;
        protected Repeater rptOtherLang;
        /// <summary>
        /// 当前资源页信息
        /// </summary>
        protected TResourcesPageModel ResPage;
        /// <summary>
        /// 当前启用资源列表
        /// </summary>
        private List<TResourcesModel> ResourceList = new List<TResourcesModel>();
        /// <summary>
        /// 管理员列表
        /// </summary>
        protected List<Model.WWW_User.User.MemberSimpleModel> AdminList = new List<Model.WWW_User.User.MemberSimpleModel>();
        /// <summary>
        /// 使用是它语言学习语言资源
        /// </summary>
        private List<TResourcesPageModel> UseOtherList;
        /// <summary>
        /// IE title文本
        /// </summary>
        protected System.Web.UI.WebControls.Literal litPageTitle;
        /// <summary>
        /// 当前会员号
        /// </summary>
        protected string memberno = "";
        protected override void OnLoad(EventArgs e)
        {
            memberno = Page.User.Identity.Name;
            LearnL = Request["learn"];
            SpeakL = Request["speak"];
            if (LearnL == "" || SpeakL == "")
            {
                Page.Response.End();
                return;
            }
            //从XML读取管理员列表，获取当前管理员
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("/Control/ResourcesRight.xml"));
            DataRow[] drs = ds.Tables[0].Select("M='" + memberno + "'");
            if (drs.Length > 0)
            {
                if (((drs[0][1].ToString().ToLower().IndexOf("|all|") > -1 || drs[0][1].ToString().ToLower().IndexOf("|" + LearnL.ToLower() + "|") > -1) && (drs[0][2].ToString().ToLower().IndexOf("|all|") > -1 || drs[0][2].ToString().ToLower().IndexOf("|" + SpeakL.ToLower() + "|") > -1)))
                {
                    IsAdmin = 1;
                }
            }
            drs = ds.Tables[0].Select("L like '%|" + LearnL + "|%' and B like '%|" + SpeakL + "|%'");
            string strAdmin = "";
            for (int i = 0; i < drs.Length; i++)
            {
                strAdmin += drs[i][0].ToString();
            }
            //0：TResourcesPageModel当前资源页信息，1：List(TResourcesModel)当前资源列表，
            //2：List(MemberSimpleModel)管理员列表信息)，3：List(TResourcesPageModel)使用其他语言学习语言资源
            ArrayList outList;
            Model.CommonMessage commonMessage = Resources.GetResourcesList(LearnL,SpeakL,strAdmin,out outList);
            ResPage = outList[0] as TResourcesPageModel;
            ResourceList = outList[1] as List<TResourcesModel>;
            AdminList = outList[2] as List<Model.WWW_User.User.MemberSimpleModel>;
            UseOtherList = outList[3] as List<TResourcesPageModel>;
            //从XML读取资源类型
            DataTable dt = GetDTFromXml("ResourcesCat.xml");
            if (rptCat != null)
            {
                rptCat.DataSource = dt;
                rptCat.DataBind();
            }
            if (rptCatForRes != null)
            {
                rptCatForRes.DataSource = dt;
                rptCatForRes.DataBind();
            }
            if (rptAdmin != null)
            {
                rptAdmin.DataSource = AdminList;
                rptAdmin.DataBind();
            }
            if (rptOtherLang != null)
            {
                rptOtherLang.DataSource = UseOtherList;
                rptOtherLang.DataBind();
            }
            //if (litPageTitle != null)
            //{
              //  ((Lx.Web.Common.UI.TDTPageBase)(this.Page)).metaTitle.Text = litPageTitle.Text.Replace("{0}", ResPage.PageTitle);
            //}
            //else
            //{
              //  ((Lx.Web.Common.UI.TDTPageBase)(this.Page)).metaTitle.Text = ResPage.PageTitle;
            //}
        }
        /// <summary>
        /// 获取分类资源
        /// </summary>
        /// <param name="obj">资源类型（CateID）</param>
        /// <returns>List(TResourcesModel)</returns>
        protected List<TResourcesModel> GetResDataSource(object obj)
        {
            int catId = -1;
            if (obj != null)
            {
                catId = Convert.ToInt32(obj);
            }
            List<TResourcesModel> listtmp = new List<TResourcesModel>();
            foreach (TResourcesModel item in ResourceList)
            {
                if (item.CateID == catId)
                {
                    listtmp.Add(item);
                }
            }
            return listtmp;
        }
    }
}
